package com.example.xinding;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {

    private static final String TAG = "FirstActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        Button btn = findViewById(R.id.button_1);
        btn.setOnClickListener(v -> {
//            Toast.makeText(FirstActivity.this, "You clicked Button 1", Toast.LENGTH_SHORT).show();
//            finish();
//            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            Intent intent = new Intent("com.example.xinding.ACTION_START");
            intent.addCategory("com.example.activitytest.MY_CATEGORY");
            intent.putExtra("extra_data", "hello second activity");
//            startActivity(intent);
            startActivityForResult(intent, 1);
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String data_return = data.getStringExtra("data_return");
                    Log.d(TAG, "onActivityResult: 接收到的返回值为:" + data_return);
                }
                break;
            default:
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (item.getItemId() == R.id.add_item){
            Toast.makeText(this, "点击了Add", Toast.LENGTH_SHORT).show();
        }else if (item.getItemId() == R.id.remove_item){
            Toast.makeText(this, "点击了Remove", Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}